package com.example.myowntodo.fet_xhttpdemo.manager

import com.example.myowntodo.fet_xhttpdemo.entity.User
import com.xuexiang.xutil.common.RandomUtils
import com.xuexiang.xutil.common.StringUtils


class UserManager private constructor() {
    var user: User? = null
        private set

    fun selectUser(user: User?) {
        this.user = user
    }

    val selectUserName: String
        get() = if (user != null) {
            user!!.name
        } else {
            "未选择用户！"
        }

    fun clear() {
        user = null
    }

    val randomUser: User
        get() {
            var user = User()
            user.age = StringUtils.toInt(RandomUtils.getRandomNumbers(2), 0)
            user.gender = (Math.random() * 2 + 1).toInt()
            user.phone = RandomUtils.getRandomNumbers(11)
            user.name = RandomUtils.getRandomLowerCaseLetters((Math.random() * 8 + 8).toInt())
            user.loginName = user.name
            user.password = RandomUtils.getRandomLowerCaseLetters((Math.random() * 10 + 8).toInt())
            return user
        }

    companion object {
        private var sInstance: UserManager? = null


        val instance: UserManager?
            get() {
                if (sInstance == null) {
                    synchronized(UserManager::class.java) {
                        if (sInstance == null) {
                            sInstance = UserManager()
                        }
                    }
                }
                return sInstance
            }
    }
}